home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / PascalString / PascalStrings.h < prev    next >
Encoding:
Text File  |  1994-11-11  |  2.6 KB  |  59 lines  |  [TEXT/MMCC]

  1. // View this file in Geneva 9 with 4 space tabs.
  2. // This code was written by François Pottier (pottier@dmi.ens.fr) and compiled with CodeWarrior 4.5. You can use it freely.
  3.  
  4. #pragma once
  5.  
  6. /*
  7. This class lets the user operate directly and transparently on Pascal strings. C strings are evil, and dealing with Pascal strings
  8. all the time is cooler. Of course, that's just my point of view.
  9. The class is designed so that you can use a PascalString anywhere instead of a Str255, transparently. PascalStrings have the
  10. cool + and += operators, too.
  11.  
  12. The PascalString class has automatic conversions to and from Str255 ( = StringPtr), OSType and long.
  13. It also has string copy and string concatenation.
  14. The class has a simple design ; it is not meant to be very efficient. It could be easily sped up and extended, but I think it's a
  15. starting point to show a few interesting aspects of C++.
  16. */
  17.  
  18. class PascalString {
  19.         Str255        S;
  20.     public:
  21.         PascalString ();                                            // Default constructor
  22.         PascalString (const Str255& source);                            // Conversion Str255 -> class
  23.         PascalString (OSType source);                                    // Conversion OSType -> class
  24.         PascalString (long source);                                    // Conversion long -> class
  25.         PascalString (const PascalString& source);                        // Initialization with copy
  26.         
  27.         operator StringPtr () { return S; };                                // Conversion class -> Str255
  28.         operator OSType ();                                            // Conversion class -> OSType
  29.         operator long ();                                            // Conversion class -> long
  30.         
  31.         PascalString& operator= (const PascalString& source);                // Assignment
  32.         PascalString& operator= (const Str255& source);                    // Assignment from Str255
  33.         PascalString& operator+= (const PascalString& source);                // Concatenation
  34.         PascalString& operator+= (const Str255& source);                    // Concatenation from Str255
  35.         
  36.     friend PascalString operator+ (const PascalString&, const PascalString&);
  37. };
  38.  
  39. // As a bonus, here a few string utilities.
  40.  
  41. void StringCopy (const unsigned char *source, StringPtr dest);                    // Copies a Pascal string into another
  42. void String2Text (const unsigned char *source, StringPtr dest);                // Copies a Pascal string into a raw text buffer
  43. short PStringCmp (StringPtr S1, StringPtr S2);                            // Compares two Pascal strings
  44.  
  45. // Useful macros to copy strings. Use them if you only have a few string copies to do and don't want to use the PascalString class
  46. // for such a little job.
  47.  
  48. typedef struct {                                                    // Kluge to create efficient code for PStrCopy31
  49.     Str31        S;
  50. } Dummy31Rec, *Dummy31Ptr;
  51.  
  52. #define    PStrCopy31(S, T)        * (Dummy31Ptr) (T) = * (Dummy31Ptr) (S)
  53.  
  54. typedef struct {
  55.     Str255        S;
  56. } Dummy255Rec, *Dummy255Ptr;
  57.  
  58. #define    PStrCopy255(S, T)        * (Dummy255Ptr) (T) = * (Dummy255Ptr) (S)
  59.